﻿# CMakeList.txt: NibiruStudioProject 的 CMake 项目，在此处包括源代码并定义
# 项目特定的逻辑。
# ！！！！注意！！！！！
# 不要直接修改CMakeLists.txt，请在CMake/ProjectXXXCustom.cmake中添加自定义代码
# CMakeLists.txt，CMake/IDE, CMake/Internal都可能在IDE运行时或者版本升级时被覆盖

cmake_minimum_required (VERSION 3.8)
#set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
#SET (CMAKE_C_COMPILER_WORKS 1)
#SET (CMAKE_CXX_COMPILER_WORKS 1)
project ("NibiruStudioProject")

message("CMake Host Arch: " ${CMAKE_HOST_SYSTEM_PROCESSOR})
message("CMake Host OS: " ${CMAKE_SYSTEM_NAME})
message("CMake Target OS: " ${TARGET_OS})
message("CMake Target Arch: " ${TARGET_ARCH})

set(CMAKE_CXX_STANDARD 17)

option(NS_DOUBLE_SUPPORT "Enable Double Precision Support" OFF)

if("${ANDROID_ABI}" STREQUAL "arm64-v8a" OR "${ANDROID_ABI}" STREQUAL "armeabi-v7a")
    set(TARGET_OS android)
     message("CMake Find Host OS: android")
endif()

if("${TARGET_OS}" STREQUAL "android")
    set(TARGET_ARCH ${ANDROID_ABI})
elseif("${TARGET_OS}" STREQUAL "")
    if(WIN32)
        set(TARGET_OS windows)
    elseif(UNIX)
        set(TARGET_OS linux)
    endif()
    message("CMake Find Host OS: " ${TARGET_OS})
endif()

if("${TARGET_ARCH}" STREQUAL "")
    if("${TARGET_OS}" STREQUAL "windows")
        set(TARGET_ARCH x64)
    elseif(${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "aarch64")
        set(TARGET_ARCH aarch64)
    else()
        set(TARGET_ARCH x64)
    endif()
    message("CMake Find Host ARCH: " ${TARGET_ARCH})
endif()

set(ENGINE_ROOT_PATH "")
if( EXISTS "${PROJECT_SOURCE_DIR}/engine.path" )
    file (STRINGS "${PROJECT_SOURCE_DIR}/engine.path" ENGINE_ROOT_PATH)
endif()

set(PROJ_CONFIG "NSProject")
if( EXISTS "${PROJECT_SOURCE_DIR}/project.config" )
	file(STRINGS "${PROJECT_SOURCE_DIR}/project.config" PROJ_CONFIG)
endif()

string(REPLACE "|" ";" PROJ_CONFIG_LIST ${PROJ_CONFIG})

foreach(VAR ${PROJ_CONFIG_LIST})
    message(">>>>>>>>> VAR: " ${VAR})
    if("${VAR}" STREQUAL "NS_DOUBLE")
        set(NS_DOUBLE_SUPPORT ON)
    endif()
endforeach()

if(NS_DOUBLE_SUPPORT)
    if("${TARGET_OS}" STREQUAL "windows")
        set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /DENGINE_DOUBLE_SUPPORT")
    else()
        list(INSERT PLATFORM_DEFINITIONS 0 "ENGINE_DOUBLE_SUPPORT")
    endif()
    message(">>>>>>>>> NIBIRU_ENGINE::NFloat is double")
else()
    message(">>>>>>>>> NIBIRU_ENGINE::NFloat is float")
endif()

#option(NS_BUILD_RUNTIME "Build Project Runtime" OFF)
option(NS_IDE_BUILD "Build Project in IDE" OFF)
option(NS_BUILD_ONE_LIB "Build into one Lib" OFF)
option(NS_EXPORT_LIB "Export as Lib" OFF)

set(DIR_ROOT ${PROJECT_SOURCE_DIR})
set(TARGET_ABI ${TARGET_OS}-${TARGET_ARCH})

#OR NOT ${NS_BUILD_RUNTIME}
if ("${TARGET_OS}" STREQUAL "android" )
    set(PROJECT_NAME NSProject)
    set(NS_BUILD_ONE_LIB ON)
else()
    list(GET PROJ_CONFIG_LIST 0 PROJECT_NAME)
endif()

if( ${NS_DEBUG_PROJECT} )
    set(DEBUG_PROJECT_INTERNAL ON)
else()
    set(DEBUG_PROJECT_INTERNAL OFF)
endif()

if(NOT DEFINED ${NS_CROSS_DEBUG})
    option(NS_CROSS_DEBUG "NS_CROSS_DEBUG" OFF)
endif()

if( ${DEBUG_PROJECT_INTERNAL} )
    set(ENGINE_ROOT_PATH ${PROJECT_SOURCE_DIR}/../../Engine)
    set(CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}/DebugProjects/${PROJECT_NAME})
elseif( "${ENGINE_ROOT_PATH}" STREQUAL "" )
    if ("${TARGET_OS}" STREQUAL "android")
        set( ENGINE_ROOT_PATH ${DIR_ROOT}/../../../libs/NSEngineLib)
    elseif ("${TARGET_OS}" STREQUAL "linux")
        set( ENGINE_ROOT_PATH ${DIR_ROOT}/Libs/NSEngineLib/)
    endif()
elseif( ${NS_CROSS_DEBUG} AND "${TARGET_OS}" STREQUAL "linux" )
    set( ENGINE_ROOT_PATH ${DIR_ROOT}/Platform/linux/NSEngineLib/)
endif()

message("NS ENGINE HOME PATH: " ${ENGINE_ROOT_PATH})
set(DIR_INC ${ENGINE_ROOT_PATH}/include)

set(DIR_LIB ${ENGINE_ROOT_PATH})
set(DIR_SRC ${PROJECT_SOURCE_DIR}/Source)
set(DIR_PROJECT_LIB ${PROJECT_SOURCE_DIR}/Libs)

message(">>>>>>>>>>>>>>> ENGINE  Root: " ${ENGINE_ROOT_PATH})
message(">>>>>>>>>>>>>>> PROJECT Name: " ${PROJECT_NAME})
message(">>>>>>>>>>>>>>> PROJECT ABI: " ${TARGET_ABI})
message(">>>>>>>>>>>>>>> PROJECT Source: " ${DIR_SRC})
message(">>>>>>>>>>>>>>> PROJECT Include: " ${DIR_INC})
message(">>>>>>>>>>>>>>> PROJECT Lib: " ${DIR_PROJECT_LIB})
message(">>>>>>>>>>>>>>> PROJECT Build Dir: " ${CMAKE_BINARY_DIR})
message(">>>>>>>>>>>>>>> PROJECT Build In IDE: " ${NS_IDE_BUILD})
message(">>>>>>>>>>>>>>> PROJECT Build Lib: " ${NS_BUILD_ONE_LIB})
message(">>>>>>>>>>>>>>> PROJECT Export Lib: " ${NS_EXPORT_LIB})
message(">>>>>>>>>>>>>>> PROJECT Cross Debug: " ${NS_CROSS_DEBUG})
message(">>>>>>>>>>>>>>> PROJECT Install Prefix: " ${CMAKE_INSTALL_PREFIX})
message(">>>>>>>>>>>>>>> PROJECT Debug Internal: " ${DEBUG_PROJECT_INTERNAL})

include_directories(${DIR_INC})

if ("${TARGET_OS}" STREQUAL "linux")
    include(${PROJECT_SOURCE_DIR}/CMake/Internal/PlatformLinux.cmake)
elseif ("${TARGET_OS}" STREQUAL "android")
    include(${PROJECT_SOURCE_DIR}/CMake/Internal/PlatformAndroid.cmake)
elseif("${TARGET_OS}" STREQUAL "windows")
    include(${PROJECT_SOURCE_DIR}/CMake/Internal/PlatformWindows.cmake)
    set(EXE_TARGET WIN32)
endif()

if( EXISTS "${PROJECT_SOURCE_DIR}/CMake/IDE/ProjectCompileIDE.cmake" )
    include(${PROJECT_SOURCE_DIR}/CMake/IDE/ProjectCompileIDE.cmake)
endif()

if( EXISTS "${PROJECT_SOURCE_DIR}/CMake/IDE/ProjectIncludeIDE.cmake" )
    include(${PROJECT_SOURCE_DIR}/CMake/IDE/ProjectIncludeIDE.cmake)
endif()

#ADD CUSTOM PLATFORM-INDEPENDENT SOURCE LIST AND INCLUDE LIST in CMake/ProjectCompileCustom.cmake
include(${PROJECT_SOURCE_DIR}/CMake/ProjectCompileCustom.cmake)

list(APPEND SOURCE_LIST 
${PLATFORM_SOURCE}
${PROJECT_SOURCE}
${PROJECT_SOURCE_CUSTOM}
)

if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug" OR "${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo")
    list(INSERT PLATFORM_DEFINITIONS 0 "NSDEBUG")
endif()

if (${NS_BUILD_ONE_LIB})
    if(${SUPPORT_CUDA})
        cuda_add_library(${PROJECT_NAME} SHARED
            ${SOURCE_LIST}
        )
    else()
       add_library(${PROJECT_NAME} SHARED
            ${SOURCE_LIST}
        )
    endif()
else()
    if(${SUPPORT_CUDA})
        cuda_add_executable (${PROJECT_NAME} ${EXE_TARGET}
            ${PLATFORM_SOURCE_MAIN}
        )
        cuda_add_library(NSProject SHARED 
            ${SOURCE_LIST}
        )
    else()
        if (NOT ${NS_EXPORT_LIB})
            add_executable (${PROJECT_NAME} ${EXE_TARGET}
                ${PLATFORM_SOURCE_MAIN}
              "Source/PosUtil.cpp" "Source/PosUtil.h")
        endif()
        add_library(NSProject SHARED 
            ${SOURCE_LIST}
          "Source/PosUtil.cpp" "Source/PosUtil.h")
    endif()

    if (NOT ${NS_EXPORT_LIB})
        target_compile_definitions(${PROJECT_NAME} PRIVATE  ${PLATFORM_DEFINITIONS} ${EXECUTABLE_CUSTOM_DEFINITIONS})
    endif()

    target_compile_definitions(NSProject PRIVATE  ${PLATFORM_DEFINITIONS} ${PROJECT_CUSTOM_DEFINITIONS})
	
    message("PLATFORM_DEFINITIONS: " ${PLATFORM_DEFINITIONS})
	message("PROJECT_CUSTOM_DEFINITIONS: " ${PROJECT_CUSTOM_DEFINITIONS})
	message("EXECUTABLE_CUSTOM_DEFINITIONS: " ${EXECUTABLE_CUSTOM_DEFINITIONS})
endif()


list(APPEND INC_LIST 
${PROJECT_TARGET_INCLUDE}
${PLATFORM_TARGET_INCLUDE}
)

if (NOT ${NS_EXPORT_LIB})
    target_include_directories(
            ${PROJECT_NAME}
            PUBLIC
            ${INC_LIST}
    )
endif()

target_include_directories(
        NSProject
        PUBLIC
        ${INC_LIST}
)

if( NOT ${NS_BUILD_ONE_LIB} AND NOT ${NS_EXPORT_LIB})
target_link_libraries( # Specifies the target library.
    ${PROJECT_NAME}
    NSProject
    ${PLATFORM_LINK_LIBS}
)
endif()

target_link_libraries( # Specifies the target library.
    NSProject
    ${PLATFORM_LINK_LIBS}
)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PLATFORM_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${PLATFORM_LINK_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${PLATFORM_LINK_FLAGS}")

if( ${NS_IDE_BUILD} OR ${DEBUG_PROJECT_INTERNAL} OR ${NS_CROSS_DEBUG})
    #INSTALL
    if ("${TARGET_OS}" STREQUAL "linux")
        include(${PROJECT_SOURCE_DIR}/CMake/Internal/InstallLinux.cmake)
    elseif("${TARGET_OS}" STREQUAL "windows")
        include(${PROJECT_SOURCE_DIR}/CMake/Internal/InstallWindows.cmake)
    endif()
endif()
